home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / hips / sources / tools / multi_mask.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-20  |  4.0 KB  |  197 lines

  1. /*    Copyright (c)    1990    Jin, Guojun
  2. *
  3. *    MULTI_MASK . C
  4. *
  5. *    Automatically pipe in & out.
  6. *        -p will make no pipe out and send data to a file with
  7. *        mask coded extension.
  8. */
  9.  
  10. #include "header.def"
  11. #include "imagedef.h"
  12. #include <math.h>
  13.  
  14. U_IMAGE    uimg;
  15.  
  16. #define    inbuf    uimg.src
  17.  
  18. typedef    union    {
  19.     char    BYTE[4];
  20.     short    INTEG[2];
  21.     long    LINT;
  22.     }BMap;
  23.  
  24. typedef    union    {
  25.     char    Byte[2];
  26.     short    Inte;
  27.     }onion;
  28.  
  29. onion    sWap;
  30. char    fname[32]={NULL}, PixBit='B', header=0, opt='&',
  31.     npip, tmp, ct, buf[64], usage[]="options\n\
  32. multi_mask [-mBit_Mask] [-h] [-BSFLIW] [-o^&o-x+] file_name [-p]\n";
  33.  
  34. BMap    Map;
  35.  
  36. BMap    *Mask(Lnt, mask)
  37. register long    Lnt, mask;
  38. {
  39. BMap    TPBM;
  40. /*fprintf(stderr,"Value = %x,    Mask = %x,    ", Lnt, mask);*/
  41. switch(opt)    {
  42.     case '&':    TPBM.LINT = Lnt & mask;    break;
  43.     case '^':    TPBM.LINT = Lnt ^ mask;    break;
  44.     case '+':    TPBM.LINT = Lnt + mask;    break;
  45.     case '-':    TPBM.LINT = Lnt - mask;    break;
  46.     case 'x':    TPBM.LINT = Lnt * mask;    break;
  47.     case 'o':    TPBM.LINT = Lnt | mask;    break;
  48.     default:    perror("Wrong Operation");    exit('o');
  49.     }
  50. /*fprintf(stderr,"Result = %x    ", TPBM.LINT);*/
  51. return    &TPBM;
  52. }
  53.  
  54. main(argc, argv)
  55. int    argc;
  56. char    *argv[];
  57. {
  58. short    nt;
  59. long    lnt;
  60. byte    I;
  61. char    ch;
  62.  
  63. format_init(&uimg, IMAGE_INIT_TYPE, HIPS, -1, *argv, "S20-1");
  64.  
  65. Map.LINT = -1;
  66. if (argc == 1)    goto    DFP;
  67. message("BYTE = %d,    INTG = %d,    LONG = %ld\n",
  68.     Map.BYTE[3], Map.INTEG[1], Map.LINT);
  69.  
  70. for (ct=1; ct<argc; ct++)    /* get all command line arguments    */
  71. {
  72.    if (*argv[ct] == '-')
  73.     {
  74.     argv[ct]++;
  75.     ch = *argv[ct];
  76.     argv[ct]++;
  77.     switch(ch)
  78.     {
  79.     case 'm':
  80.         while(isdigit(*argv[ct]))
  81.         {
  82.         long    ltmp;
  83.             if (*(argv[ct]++) == '1')
  84.                 ltmp = 1;
  85.             else    ltmp = 0;
  86.             Map.LINT <<= 1;
  87.             Map.LINT |= ltmp;
  88.         }
  89.         break;
  90.     case 'x':
  91.         sscanf(argv[ct], "%lx", &Map.LINT);
  92.         break;
  93.     case 'B':
  94.     case 'S':
  95.     case 'I':
  96.     case 'F':
  97.     case 'W':
  98.     case 'L':    PixBit = ch;    break;
  99.     case 'h':    header = True;    break;
  100.     case 'o':    opt = *argv[ct];break;
  101.     case 'p':    npip = 1;    break;
  102.     default:
  103. DFP:        usage_n_options(usage, ct, argv[ct]);
  104.     }/* end switch    */
  105.     }/* end if    */
  106.     else    strcpy(fname, argv[ct]);    /* get file name    */
  107. }/* end for    */
  108.  
  109. io_test(stdin_fd, goto    DFP);
  110.  
  111. message("BYTE = %d, INTG = %d, LONG = %ld\n",
  112.     Map.BYTE[3], Map.INTEG[1], Map.LINT);
  113.  
  114. if (strlen(fname)==0)
  115. #ifdef    IBM_PC
  116. {    fprintf(stderr,"Input Orginal File Name ");    gets(fname);    }
  117. #else
  118. ;else
  119. #endif
  120. if ((in_fp=freopen(fname, "rb", stdin)) == NULL)
  121. {
  122. rerr:    perror("open reading file error");    exit('i');
  123. }
  124.  
  125. if (npip)
  126. {    sprintf(buf, ".%c%x", opt + 0x20, Map.LINT);    strcat(fname, buf);
  127.     if ((out_fp=freopen(fname, "wb", stdout)) == NULL)
  128.     {    perror("open writing file error");    exit('o');    }
  129. }
  130.  
  131. io_test(fileno(in_fp), goto    DFP);
  132.  
  133. if (header) {    /*    pass hips header
  134.     for (ch=0; ch < 100 && *buf != '.'; ch++)    {
  135.         if (!fgets(buf, 80, in_fp))    goto    rerr;
  136.         fputs(buf, out_fp);
  137.     }    */
  138.     (*uimg.header_handle)(HEADER_READ, &uimg, 0, 0);
  139.     (*uimg.header_handle)(HEADER_WRITE, &uimg, argc, argv, True);
  140. }
  141.  
  142. fprintf(stderr, "Header = %d,    PixBit = %c,    BitMap = %x, OPT = %c\n",
  143.     header, PixBit, Map.LINT, opt);
  144. switch(PixBit)
  145. {    case 'B':
  146.         while(True)
  147.         {
  148.         fread(&ch, sizeof(ch), 1, in_fp);
  149.         if (feof(in_fp))    break;
  150.         ch = Mask((long)ch, (long)Map.BYTE[3])->BYTE[3];
  151. /*fprintf(stderr, "RET = %x\n", ch);*/
  152.         fwrite(&ch, sizeof(ch), 1, out_fp);
  153.         }
  154.         break;
  155.     case 'S':
  156.         while(True)
  157.         {
  158.         fread(&nt, sizeof(nt), 1, in_fp);
  159.         if (feof(in_fp))    break;
  160.         nt = Mask((long)nt, (long)Map.INTEG[1])->INTEG[1];
  161. /*fprintf(stderr, "RET = %x\n", nt);*/
  162.         fwrite(&nt, sizeof(nt), 1, out_fp);
  163.         }
  164.         break;
  165.     case 'L':
  166.         while(True)
  167.         {
  168.         fread(&lnt, sizeof(lnt), 1, in_fp);
  169.         if (feof(in_fp))    break;
  170.         lnt = Mask(lnt, Map.LINT)->LINT;
  171.         fwrite(&lnt, sizeof(lnt), 1, out_fp);
  172.         }
  173.         break;
  174.     case 'I':
  175.         I = 0;
  176.         while(True)
  177.         {
  178.         fread(&tmp, sizeof(tmp), 1, in_fp);
  179.         if (feof(in_fp))    break;
  180.         I++;
  181.         tmp = Mask((long)tmp, (long)I)->BYTE[3];
  182.         fwrite(&tmp, sizeof(tmp), 1, out_fp);
  183.         }
  184.         break;
  185.     case 'W':
  186.         while(True)
  187.         {
  188.         fread(&sWap.Inte, sizeof(sWap.Inte), 1, in_fp);
  189.         if (feof(in_fp))    break;
  190.         I++;
  191.         sWap.Byte[0] = Mask((long)sWap.Byte[0], (long)sWap.Byte[1])->BYTE[3];
  192.         fwrite(&sWap.Inte, sizeof(sWap), 1, out_fp);
  193.         }
  194. }
  195. fclose(in_fp);    fclose(out_fp);
  196. }
  197.